<?php

if (!function_exists('format_date')) {
    /**
     * 将 2018-10-27 20:20:20 转化为 n小时前
     * @param $the_time
     * @return string
     * create in 2018/10/27
     */
    function format_date($the_time)
    {
        $now_time = date("Y-m-d H:i:s");
        $now_time = strtotime($now_time);
        $show_time = strtotime($the_time);
        $t = $now_time - $show_time + 1;
        $f = [
            '31536000' => '年',
            '2592000' => '个月',
            '604800' => '星期',
            '86400' => '天',
            '3600' => '小时',
            '60' => '分钟',
            '1' => '秒',
        ];
        foreach ($f as $k => $v) {
            if (0 != $c = floor($t / (int)$k)) {
                return $c . $v . '前';
            }
        }
        return false;
    }
}

if (!function_exists('attach')) {
    /**
     * 拼接域名
     * @param $text
     * @param $length
     * @return string
     */
    function attach($file_path = '')
    {
        $http_url   = request()->domain();
//        $oss_url    = config("api_cos.mmUrl");
        $file_path  = ltrim($file_path, '/');
        // url
        if (is_url($file_path)) {
            return $file_path;
        }
        // 服务器路径
        $http_file = $http_url . "/" . $file_path;
        if (file_exists(ROOT_PATH . "public/" . $file_path)) {
            return $http_file;
        }
        // 云储存路径
//        $oss_file = $oss_url . '/' . $file_path;
//        if (cos_object_is_exist($file_path)) {
//            return $oss_file;
//        }
        //  最后的意外
        return $http_url . "/static/nopic.png";
    }
}

if (!function_exists('is_url')) {
    /**
     * 检测是否域名
     * @param $value
     * @param bool $is 检测是否非纯域名
     * @return bool
     */
    function is_url($value, $is = true)
    {
        // url
        if (preg_match('/^http(s)?:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is', $value)) {
            if ($is) {
                return !!@trim(parse_url($value)["path"], "/");
            }
            return true;
        }
        return false;
    }
}


if (!function_exists('detach')) {
    /**
     * 剪切域名
     * @param string $url_path
     * @return string
     */
    function detach($url_path = "")
    {
        if (preg_match('/^http(s)?:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is', $url_path)) {
            return @parse_url($url_path)["path"];
        } else {
            return $url_path;
        }
    }
}

if (!function_exists('root_attach')) {
    /**
     * 绝对路径
     * @param $path
     * @return string|null
     */
    function root_attach($path)
    {
        if ($path) {
            return ROOT_PATH . "public/" . detach($path);
        }
        return null;
    }
}

if (!function_exists('path_handle')) {
    /**
     * 定义目录信息
     * @param string $path
     * @param string $name
     * @return array
     */
    function path_handle($path = "", $name = "")
    {
        // 默认地址
        $path = $path ?: 'upload/default/' . date('Y/m/d');
        // 唯一昵称
        $name = $name ?: uniqid() . ".jpg";
        // 真实目录
        $root_dir = ROOT_PATH . "public/" . $path;
        !is_dir($root_dir) && @mkdir(iconv("UTF-8", "GBK", $root_dir)
            , 0777, true);
        // 相对目录
        $show_dir = ROOT_DIR . $path;
        // 真实路径
        $save_path = $root_dir . $name;
        // 相对路径
        $show_path = ROOT_DIR . $path . $name;

        return compact("root_dir", "show_dir", "save_path", "show_path");
    }
}

if (!function_exists('subtext')) {
    /**
     * 截取省略字符串
     * @param $text
     * @param $length
     * @return string
     */
    function subtext($text, $length)
    {
        if (mb_strlen($text, 'utf8') > $length)
            return mb_substr($text, 0, $length, 'utf8') . '...';
        return $text;
    }
}

if (!function_exists('array_sort')) {
    /**
     * 二维数组根据某个字段排序
     * @param array $array 要排序的数组
     * @param string $keys 要排序的键字段
     * @param string $sort 排序类型  SORT_ASC     SORT_DESC
     * @return array 排序后的数组
     */
    function array_sort($array, $keys, $sort = SORT_DESC)
    {
        $keysValue = [];
        foreach ($array as $k => $v) {
            $keysValue[$k] = $v[$keys];
        }
        array_multisort($keysValue, $sort, $array);
        return $array;
    }
}


if (!function_exists('calculate_age')) {
    /**
     * 计算年龄
     * @param $birthday
     * @param bool $actuarial
     * @return array|string
     */
    function calculate_age($birthday, $actuarial = false)
    {
        if ($birth_time = strtotime($birthday)) {
            $birthday = date("Y-m-d", $birth_time);
            $now_day = date("Y-m-d");

            list($birth_year, $birth_month, $birth_day) = explode('-', $birthday);
            list($now_year, $now_month, $now_day) = explode('-', $now_day);

            $year = $now_year - $birth_year;
            $month = $now_month - $birth_month;
            $day = $now_day - $birth_day;

            // 进制
            if ($day < 0) {
                $day += (int)date('t', strtotime("-1 month " . $now_day));
                $month--;
            }

            if ($month < 0) {
                $month += 12;
                $year--;
            }

            if ($actuarial) {
                if ($year > 0) {
                    $dot = round($month / 12, 1);
                    $rs = ($year + $dot) . "岁";
                } else {
                    if ($month > 0) {
                        $rs = $month . "个月";
                    } else {
                        $rs = ($day + 1) . " 天";
                    }
                }
                return $rs;
            } else {
                return $year . " 岁";
            }
        } else {
            return '';
        }
    }
}
